package cn.mw.cmdb.entity;

import cn.mw.cmdb.param.QueryMonitorParam;
import cn.mw.cmdb.service.ServiceChoose;
import cn.mw.cmdb.service.impl.InstanceMonitorServiceImpl;
import cn.mw.components.mongodb.anno.AutoId;
import cn.mw.components.mongodb.anno.CMDBProperty;
import cn.mw.microMonitorCommon.security.dto.MwUser;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.google.common.base.Strings;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.*;
import java.util.stream.Collectors;

import static cn.mw.cmdb.util.ValConvertUtil.strValueConvert;

/**
 * 监控布局
 */
@Data
@Document("mw_component_layout_table")
public class MwComponentLayoutInfo extends QueryMonitorParam implements ServiceChoose {
    @CMDBProperty
    @AutoId
    @Schema(description = "id")
    protected String id;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "用户Id")
    private String userId;

    @CMDBProperty
    @Schema(description = "布局数据")
    private List<MwTabLayoutInfo> componentLayout;

    @CMDBProperty
    @Schema(description = "监控Tab标签集合")
    private List<MwLayoutLabelInfo> labels;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "模板id")
    private String tempId;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "监控服务器id")
    private String serverId;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "资产实例id")
    private String assetsId;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "模型id")
    private String modelId;

    @CMDBProperty(searchType = PropertyTypeSwitch.ID)
    @Schema(description = "是否数据解析处理")
    private Boolean resolved;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    @Schema(description = "模板名称")
    private String tempName;
    @Override
    @Schema(description = "模型服务Id")
    public String getServiceId() {
        return InstanceMonitorServiceImpl.ID;
    }

    public void setUserIdInfo() {
        //获取当前用户
        MwUser user = SecurityUtils.getUser();
        String userId = strValueConvert(user.getId());
        this.userId = userId;
    }

    /**
     * 获取所有需要解析的监控项名称
     *
     * @return
     */
    public Set<String> getAllItemNameSet() {
        Set<String> allItemNameSet = new HashSet<>();
        if (CollectionUtils.isNotEmpty(componentLayout)) {
            for (MwTabLayoutInfo layoutInfo : componentLayout) {
                //获取未解析的布局数据，resolve为false
                if (!layoutInfo.isResolved()) {
                    if (CollectionUtils.isNotEmpty(layoutInfo.getLayout())) {
                        for (MwLayoutDetailInfo detailInfo : layoutInfo.getLayout()) {
                            Set<String> allItemName = detailInfo.getItemNames();
                            if (CollectionUtils.isNotEmpty(allItemName)) {
                                allItemNameSet.addAll(allItemName);
                            }
                        }
                    }
                }
            }
        }
        return allItemNameSet;
    }

    /**
     * 根据监控项名称设置itemId
     *
     * @return
     */
    public void setAllItemIdByName(Map<String, Object> itemInfoMap) {
        if (CollectionUtils.isNotEmpty(componentLayout)) {
            for (MwTabLayoutInfo layoutInfo : componentLayout) {
                if (CollectionUtils.isNotEmpty(layoutInfo.getLayout())) {
                    for (MwLayoutDetailInfo detailInfo : layoutInfo.getLayout()) {
                        Set<String> allItemName = detailInfo.getItemNames();
                        if (CollectionUtils.isNotEmpty(allItemName)) {
                            detailInfo.setItemIdByName(itemInfoMap);
                        }
                    }
                    //解析过得布局数据设置为true
                    layoutInfo.setResolved(true);
                }
            }
        }
    }

    public void resettingResolved() {
        if (CollectionUtils.isNotEmpty(componentLayout)) {
            for (MwTabLayoutInfo layoutInfo : componentLayout) {
                layoutInfo.setResolved(false);
            }
        }
    }

    public Map<String, MwTabLayoutInfo> getLayoutInfoByTabId() {
        Map<String, MwTabLayoutInfo> collect = new HashMap<>();
        if (CollectionUtils.isNotEmpty(componentLayout)) {
            collect = componentLayout.stream().collect(Collectors.toMap(s -> s.getLabelId(), s -> s, (o1, o2) -> o1));
        }
        return collect;
    }
}
